﻿#include "yaw.h"

static int r = 250;
static int X = 4 * r;//窗口长
static int Y = 3 * r; //窗口宽
static int x = 2 * r;//盘的横坐标
static int y = 8 * r / 5; //盘的纵坐标

void drawYaw(int x0, int y0, int r0, double yaw)
{ 
	x = x0;
	y = y0;
	r = r0;
	ege_enable_aa(true); //开启抗锯齿
	//double angle = engineRPM - 13;
	double angle = yaw;
	if (angle <= 0) {
		angle = 0;
	}
	double radin = angle - 3.845;
	//double radin = (angle / 10 * 260 / 35) * PI / 180 - PI * 5 / 4;
	//for (; is_run(); delay_fps(30)) {
	//	cleardevice();
	settextjustify(LEFT_TEXT, TOP_TEXT);
	draw_kuangYaw();
	draw_frameworkYaw();
	draw_pointerYaw(radin);
	//numberPanelPHI();
}
void draw_frameworkYaw() {
    // 画主要的圆盘
    setcolor(EGEARGB(42, 42, 42, 42));
    setfillcolor(EGEARGB(42, 42, 42, 42)); // 填充的颜色

    // 绘制刻度
    setcolor(WHITE); // 刻度颜色

    // 绘制主刻度（0-1000，每100一个刻度）
    int numMajorTicks = 11; // 0,100,...,1000 共11个主刻度
    double startAngle = 140.0; // 起始角度（0位置）
    double endAngle = 40.0;    // 结束角度（1000位置）
    double totalAngle = (startAngle > endAngle) ?
        (360.0 - startAngle + endAngle) :
        (endAngle - startAngle);
    double angleStep = totalAngle / (numMajorTicks - 1); // 每个刻度之间的角度

    // 画粗刻度（主刻度）- 调整到圆弧内部
    setlinewidth(6 * r / 250.0);
    for (int i = 0; i < numMajorTicks; i++) {
        double angle = startAngle + i * angleStep;
        if (angle > 360) angle -= 360; // 处理角度环绕

        double radian = angle * PI / 180.0; // 角度转弧度

        // 计算刻度线的起点和终点（调整为内部刻度）
        double startX = x + (r * 0.95 * cos(radian));  // 更靠近外部
        double startY = y + (r * 0.95 * sin(radian));
        double endX = x + (r * 0.72 * cos(radian));    // 更靠近中心
        double endY = y + (r * 0.72 * sin(radian));

        // 绘制刻度线
        ege_line(startX, startY, endX, endY);
    }

    // 绘制次刻度（每20一个刻度）
    int numMinorTicks = 51; // 0,20,...,1000 共51个次刻度
    double minorAngleStep = totalAngle / (numMinorTicks - 1); // 次刻度角度步长

    // 画细刻度（次刻度）- 调整到圆弧内部
    setlinewidth(3 * r / 250.0);
    for (int i = 0; i < numMinorTicks; i++) {
        // 跳过主刻度位置（每5个次刻度有一个主刻度）
        if (i % 5 == 0) continue;

        double angle = startAngle + i * minorAngleStep;
        if (angle > 360) angle -= 360; // 处理角度环绕

        double radian = angle * PI / 180.0; // 角度转弧度

        // 计算刻度线的起点和终点（调整为内部刻度）
        double startX = x + (r * 0.95 * cos(radian));  // 更靠近外部
        double startY = y + (r * 0.95 * sin(radian));
        double endX = x + (r * 0.82 * cos(radian));    // 更靠近中心
        double endY = y + (r * 0.82 * sin(radian));

        // 绘制刻度线
        ege_line(startX, startY, endX, endY);
    }

    // 绘制数字标签（主刻度值）
    setbkmode(TRANSPARENT); // 透明字体背景
    settextcolor(WHITE);

    for (int i = 0; i < numMajorTicks; i++) {
        int value = 1000 + i * 100;  // 
        double angle = startAngle + i * angleStep;
        if (angle > 360) angle -= 360;

        double radian = angle * PI / 180.0;

        // 根据角度调整标签位置
        double labelRadius = r * 0.58; // 标签位置半径
        double textX = x + labelRadius * cos(radian);
        double textY = y + labelRadius * sin(radian);

        // 设置字体大小（根据位置动态调整）
        int fontSize = (i % 2 == 0) ? r / 6.5 : r / 6.5; // 交替使用两种字体大小
        setfont(fontSize, 0, "Arial");

        // 调整文本对齐（根据角度象限）
        char str[10];
        sprintf(str, "%d", value);
        int textWidth = textwidth(str);
        int textHeight = textheight(str);

        // 特殊处理500标签（正上方）
        if (value == 500) {
            textX = x;
            textY = y - r * 1.05; // 正上方位置
            setfont(r / 8, 0, "Arial");
            ege_outtextxy(textX - textWidth / 2, textY - textHeight / 2, "500");
        }
        // 处理其他标签
        else {
            textX -= textWidth / 2;
            textY -= textHeight / 2;

            // 避开顶部和底部区域
            if (angle > 30 && angle < 150) textY -= r * 0.05;
            if (angle > 210 && angle < 330) textY += r * 0.05;

            ege_outtextxy(textX, textY, str);
        }
    }

    // 绘制标题文本
    setfont(r / 9, 0, "Arial");
    ege_outtextxy(x - r / 12, y - r / 2.6, "PWM");
    setfont(r / 10.5, 0, "Arial");//字体
    ege_outtextxy(x - r / 14, y - r / 4, "(us)");
    setfont(r / 6, 0, "Arial");
    ege_outtextxy(x - r / 7, y + r / 2, "Yaw");
}

void draw_pointerYaw(double radin) {
	setfillcolor(WHITE);
	ege_circle(x, y, r / 25);
	ege_fillcircle(x, y, r / 25);
	ege_point polypoint[] = { x + r / 1.3 * cos(radin) ,y + r / 1.3 * sin(radin), x + r / 8 * cos(radin - 150 * PI / 180),y + r / 8 * sin(radin - 150 * PI / 180), x + r / 6 * cos(radin - 180 * PI / 180), y + r / 6 * sin(radin - 180 * PI / 180), x + r / 8 * cos(radin - 210 * PI / 180),y + r / 8 * sin(radin - 210 * PI / 180) };
	ege_fillpoly(4, polypoint);
	setfillcolor(GRAY);
	ege_point polypoint1[] = { x + r / 8 * cos(radin - 150 * PI / 180),y + r / 8 * sin(radin - 150 * PI / 180),
		x + r / 6 * cos(radin - 180 * PI / 180), y + r / 6 * sin(radin - 180 * PI / 180),
		x + r / 8 * cos(radin - 210 * PI / 180),y + r / 8 * sin(radin - 210 * PI / 180),
		x + r / 6.8 * cos(radin - 210 * PI / 180),y + r / 6.8 * sin(radin - 210 * PI / 180),
		x + r / 5 * cos(radin - 180 * PI / 180), y + r / 5 * sin(radin - 180 * PI / 180),
		x + r / 6.8 * cos(radin - 150 * PI / 180),y + r / 6.8 * sin(radin - 150 * PI / 180), };
	ege_fillpoly(6, polypoint1);
}

void draw_kuangYaw() {

	int circle_r = r;
	int circle_x = x;
	int circle_y = y;

	setcolor(BLACK);//描边颜色
	setlinewidth(15 * r / 250.0);//线条粗细
	ege_circle(circle_x, circle_y, circle_r + circle_r / 10);//描边

	ege_point p5 = { circle_x - circle_r - circle_r / 10 + circle_r / 30 * 11 ,circle_y - circle_r - circle_r / 10 + circle_r / 30 * 11 };
	ege_setpattern_ellipsegradient(p5, EGERGB(160, 160, 160), circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10), EGEACOLOR(0xFF, 0x00161616));
	ege_fillrect(circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10));

	//最外圈

	setcolor(BLACK);//描边颜色
	setlinewidth(2 * r / 250.0);//线条粗细
	ege_circle(circle_x, circle_y, circle_r + circle_r * 3 / 150);//描边

	ege_point p6 = { circle_x - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 ,circle_y - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 };
	ege_setpattern_ellipsegradient(p6, EGERGB(200, 200, 200), circle_x - circle_r - circle_r / 300 * 6, circle_y - circle_r - circle_r / 300 * 6, 2 * (circle_r + circle_r / 300 * 6), 2 * (circle_r + circle_r / 300 * 6), EGEACOLOR(0xFF, 0x003A3A3C));
	ege_fillrect(circle_x - circle_r - circle_r / 300 * 6, circle_y - circle_r - circle_r / 300 * 6, 2 * (circle_r + circle_r / 300 * 6), 2 * (circle_r + circle_r / 300 * 6));
	//第二圈

	setlinewidth(14 * r / 250.0);//线条粗细
	setfillcolor(EGERGB(50, 50, 50));//填充为灰色
	ege_circle(circle_x, circle_y, circle_r - circle_r / 30);//描边

	ege_point p7 = { circle_x  ,circle_y };
	ege_setpattern_ellipsegradient(p7, EGERGB(70, 70, 70), circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30), EGEACOLOR(0xFF, 0x00292925));
	ege_fillrect(circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30));


	ege_fillcircle(circle_x, circle_y, circle_r - circle_r / 30);
	setfillcolor(EGERGB(38, 38, 38));//填充为深灰色
}
